package day2;

import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;

import org.junit.Test;

public class WhileTest {
	@Test
	public void test1() {
		//循环条件初始化
		int count = 1;
		//循环条件的控制
		while (count <= 5 ) {
			System.out.println("HelloWorld!!!");
			//循环条件的改变
			count++;
		}
	}
	
	//打印五行helloworld
	@Test
	public void test2() {
		for(int i = 1 ; i <= 5; i++ ) {
			System.out.println("helloworld!!!");
		}
	}
	
	//用FOR 和 WHILE 进行高斯求和
	@Test
	public void test3() {
		int sum1 = 0;
		int sum2 = 0;
		
		for (int i = 1; i <= 100; i++) {
			sum1 += i;
		}
		System.out.println("for循环结果是"+sum1);
		
		int count = 1;
		while (count <= 100) {
			sum2 += count;
			count++;
		}
		System.out.println("while循环结果是"+sum2);
	}
	
	
	//猜数游戏
	@Test
	public void test4() {
		
		//new Random().nextInt(300); //1~299
		int random = new Random().nextInt(300) + 1; //1~300的随机数
		System.out.println(random);
		Scanner scanner = new Scanner(System.in);
		
		while (true) {
			System.out.println("猜吧!");
			int num = scanner.nextInt();
			if (num < random) {
				System.out.println("数小了");
				
			}else if(num > random){
				System.err.println("数大了");
				
			}else if(num == random){
				System.out.println("猜对了!");
				break;
			}
			
			
		}
		
	}
	
	//统计100以内7的倍数
	@Test
	public void test5() {
		int count = 0;
		for (int i = 1; i <= 100; i++) {
			if(i % 7 == 0) {
				count++;
			}
			
		}
		System.out.println("7的倍数有"+count+"个");
	}
	
	
	//输出九九乘法表最后一行
	@Test
	public void test6() {
		for (int i = 1; i <= 9 ; i++) {
			System.out.print(i + " * 9"  + " = " + i * 9);
			System.out.print("   ");
		}			
	}
	
	//3*4的矩形
	@Test
	public void test7() {
		for (int i = 1 ; i <= 4 ; i++ ) {
			for (int j = 1; j <= 3; j++) {
				System.out.print("*");				
			}
			System.out.println();
		}
	}

	//输出直角三角形
	@Test
	public void test8() {
		for (int i = 1 ; i <= 4 ; i++ ) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");				
			}
			System.out.println();
		}
	}
	
	
	//输出99乘法表
	@Test
	public void test9() {
		for (int i = 1 ; i <= 9 ; i++) {
			for(int j = 1 ; j <= i ;j++) {
				System.out.print(j + " * "+ i + " = " +  j * i + "\t");
			}
			System.out.println();
		}
	}
	
}